package com.onlinelibrary.activities.modify;
import android.annotation.SuppressLint;
import android.app.Application;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.onlinelibrary.activities.App;
import com.onlinelibrary.databinding.ActivityModifyBinding;
import com.onlinelibrary.utils.Http;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
public class ModifyVM extends AndroidViewModel {
    public static MutableLiveData<String>email=new MutableLiveData<>(),name=new MutableLiveData<>(),stuId=new MutableLiveData<>(),department=new MutableLiveData<>(),remarks=new MutableLiveData<>();
    public static MutableLiveData<Boolean> male=new MutableLiveData<>(),female=new MutableLiveData<>(),secret=new MutableLiveData<>();
    @SuppressLint("StaticFieldLeak")
    private static ModifyActivity modifyActivity;
    private static String oldEmail="";
    public ModifyVM(@NonNull Application application) {
        super(application);
        email.setValue("");
        name.setValue("");
        stuId.setValue("");
        department.setValue("");
        remarks.setValue("");
        male.setValue(true);
        female.setValue(false);
        secret.setValue(false);
    }
    public void setBinding(ModifyActivity modifyActivity) {
        ModifyVM.modifyActivity=modifyActivity;
        modifyActivity.setResult(2);
        Http.get("user/getUserMsg/?email="+ App.sp.getString("account",""), new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) { }
            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                try {
                    assert response.body() != null;
                    JSONObject jo = new JSONObject(response.body().string());
                    name.postValue(jo.getString("name"));
                    String sex = jo.getString("gender");
                    if (sex.equals("男")){
                        male.postValue(true);
                    }else if (sex.equals("女")){
                        female.postValue(true);
                    }else {
                        secret.postValue(true);
                    }
                    oldEmail=jo.getString("email");
                    email.postValue(jo.getString("email"));
                    department.postValue(jo.getString("department"));
                    stuId.postValue(jo.getString("stu_id"));
                    remarks.postValue(jo.getString("remarks"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public static void modify(View view){
        if (!email.getValue().equals("")&&!name.getValue().equals("")&&!department.getValue().equals("")&&!remarks.getValue().equals("")){
            String gender="保密";
            if (male.getValue()){
                gender="男";
            }
            if (female.getValue()){
                gender="女";
            }
            if (secret.getValue()){
                gender="保密";
            }
            try {
                JSONObject jo=new JSONObject();
                jo.put("oldEmail",oldEmail);
                jo.put("email",email.getValue());
                jo.put("stu_id",stuId.getValue());
                jo.put("name",name.getValue());
                jo.put("department",department.getValue());
                jo.put("remarks",remarks.getValue());
                jo.put("gender",gender);
                jo.put("password",App.sp.getString("password",""));
                Http.post(jo, "user/modify/", new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e){}
                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) {
                        modifyActivity.runOnUiThread(() -> {
                            try {
                                assert response.body() != null;
                                if (response.body().string().equals("保存完毕")){
                                    modifyActivity.setResult(3);
                                    Toast.makeText(modifyActivity, "更新成功", Toast.LENGTH_SHORT).show();
                                    @SuppressLint("CommitPrefEdits") SharedPreferences.Editor edit = App.sp.edit();
                                    edit.putString("account",email.getValue());
                                    edit.apply();
                                    modifyActivity.finish();
                                }else{
                                    Toast.makeText(modifyActivity, "保存失败", Toast.LENGTH_SHORT).show();
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        });
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }else {
            Toast.makeText(modifyActivity, "请填写完整", Toast.LENGTH_SHORT).show();
        }
    }
}